package com.dianping.cat.consumer.cross.model.transform;

import com.dianping.cat.consumer.cross.model.IEntity;
import com.dianping.cat.consumer.cross.model.IVisitor;
import com.dianping.cat.consumer.cross.model.entity.CrossReport;
import com.dianping.cat.consumer.cross.model.entity.Local;
import com.dianping.cat.consumer.cross.model.entity.Name;
import com.dianping.cat.consumer.cross.model.entity.Remote;
import com.dianping.cat.consumer.cross.model.entity.Type;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;






















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  46 */   public DefaultXmlBuilder() { this(false); }



/*  50 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  54 */     this.m_compact = compact;
/*  55 */     this.m_sb = sb;
/*  56 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  60 */     entity.accept(this.m_visitor);
/*  61 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  65 */     this.m_level--;

/*  67 */     indent();
/*  68 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  72 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  76 */     if (value == null) {
/*  77 */       return null;
    }

/*  80 */     String str = toString(value);
/*  81 */     int len = str.length();
/*  82 */     StringBuilder sb = new StringBuilder(len + 16);

/*  84 */     for (int i = 0; i < len; i++) {
/*  85 */       char ch = str.charAt(i);

/*  87 */       switch (ch) {
        case '<':
/*  89 */           sb.append("&lt;");
          break;
        case '>':
/*  92 */           sb.append("&gt;");
          break;
        case '&':
/*  95 */           sb.append("&amp;");
          break;
        case '"':
/*  98 */           if (!text) {
/*  99 */             sb.append("&quot;");
            break;
          }
        default:
/* 103 */           sb.append(ch);
          break;
      }

    }
/* 108 */     return sb.toString();
  }

  protected void indent() {
/* 112 */     if (!this.m_compact) {
/* 113 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 114 */         this.m_sb.append("   ");
      }
    }
  }


/* 120 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 124 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 128 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 132 */     indent();

/* 134 */     this.m_sb.append('<').append(name);

/* 136 */     int len = nameValues.length;

/* 138 */     for (int i = 0; i + 1 < len; i += 2) {
/* 139 */       Object attrName = nameValues[i];
/* 140 */       Object attrValue = nameValues[i + 1];

/* 142 */       if (attrValue != null) {
/* 143 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 147 */     if (dynamicAttributes != null) {
/* 148 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 149 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 153 */     if (text != null && closed) {
/* 154 */       this.m_sb.append('>');
/* 155 */       this.m_sb.append(escape(text, true));
/* 156 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 158 */       if (closed) {
/* 159 */         this.m_sb.append('/');
      } else {
/* 161 */         this.m_level++;
      }

/* 164 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 170 */     if (value instanceof String)
/* 171 */       return (String)value; 
/* 172 */     if (value instanceof Collection) {
/* 173 */       Collection<Object> list = (Collection)value;
/* 174 */       StringBuilder sb = new StringBuilder(32);
/* 175 */       boolean first = true;

/* 177 */       for (Object item : list) {
/* 178 */         if (first) {
/* 179 */           first = false;
        } else {
/* 181 */           sb.append(',');
        }

/* 184 */         if (item != null) {
/* 185 */           sb.append(item);
        }
      }

/* 189 */       return sb.toString();
/* 190 */     }  if (value.getClass().isArray()) {
/* 191 */       int len = Array.getLength(value);
/* 192 */       StringBuilder sb = new StringBuilder(32);
/* 193 */       boolean first = true;

/* 195 */       for (int i = 0; i < len; i++) {
/* 196 */         Object item = Array.get(value, i);

/* 198 */         if (first) {
/* 199 */           first = false;
        } else {
/* 201 */           sb.append(',');
        }

/* 204 */         if (item != null) {
/* 205 */           sb.append(item);
        }
      }

/* 209 */       return sb.toString();
    }

/* 212 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 216 */     if (text == null) {
      return;
    }

/* 220 */     indent();

/* 222 */     this.m_sb.append('<').append(name);

/* 224 */     int len = nameValues.length;

/* 226 */     for (int i = 0; i + 1 < len; i += 2) {
/* 227 */       Object attrName = nameValues[i];
/* 228 */       Object attrValue = nameValues[i + 1];

/* 230 */       if (attrValue != null) {
/* 231 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 235 */     this.m_sb.append(">");
/* 236 */     this.m_sb.append(escape(text, true));
/* 237 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 241 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 245 */     indent();

/* 247 */     this.m_sb.append('<').append(name).append(">");

/* 249 */     if (escape) {
/* 250 */       this.m_sb.append(escape(text, true));
    } else {
/* 252 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 255 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected String toString(Date date, String format) {
/* 259 */     if (date != null) {
/* 260 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 262 */     return null;
  }


  protected String toString(Number number, String format) {
/* 267 */     if (number != null) {
/* 268 */       return (new DecimalFormat(format)).format(number);
    }
/* 270 */     return null;
  }



  public void visitCrossReport(CrossReport crossReport) {
/* 276 */     startTag("cross-report", null, new Object[] { "domain", crossReport.getDomain(), "startTime", toString(crossReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(crossReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 278 */     if (!crossReport.getDomainNames().isEmpty()) {
/* 279 */       for (String domain : crossReport.getDomainNames()) {
/* 280 */         tagWithText("domain", domain, new Object[0]);
      }
    }

/* 284 */     if (!crossReport.getIps().isEmpty()) {
/* 285 */       for (String ip : crossReport.getIps()) {
/* 286 */         tagWithText("ip", ip, new Object[0]);
      }
    }

/* 290 */     if (!crossReport.getLocals().isEmpty()) {
/* 291 */       for (Local local : crossReport.getLocals().values()) {
/* 292 */         local.accept(this.m_visitor);
      }
    }

/* 296 */     endTag("cross-report");
  }


  public void visitLocal(Local local) {
/* 301 */     startTag("local", null, new Object[] { "id", local.getId() });

/* 303 */     if (!local.getRemotes().isEmpty()) {
/* 304 */       for (Remote remote : local.getRemotes().values()) {
/* 305 */         remote.accept(this.m_visitor);
      }
    }

/* 309 */     endTag("local");
  }



/* 314 */   public void visitName(Name name) { startTag("name", true, null, new Object[] { "id", name.getId(), "totalCount", Long.valueOf(name.getTotalCount()), "failCount", Integer.valueOf(name.getFailCount()), "failPercent", toString(Double.valueOf(name.getFailPercent()), "0.00"), "avg", toString(Double.valueOf(name.getAvg()), "0.00"), "sum", toString(Double.valueOf(name.getSum()), "0.00"), "tps", toString(Double.valueOf(name.getTps()), "0.00") }); }



  public void visitRemote(Remote remote) {
/* 319 */     startTag("remote", null, new Object[] { "id", remote.getId(), "role", remote.getRole(), "app", remote.getApp(), "ip", remote.getIp() });

/* 321 */     if (remote.getType() != null) {
/* 322 */       remote.getType().accept(this.m_visitor);
    }

/* 325 */     endTag("remote");
  }


  public void visitType(Type type) {
/* 330 */     startTag("type", null, new Object[] { "id", type.getId(), "totalCount", Long.valueOf(type.getTotalCount()), "failCount", Integer.valueOf(type.getFailCount()), "failPercent", toString(Double.valueOf(type.getFailPercent()), "0.00"), "avg", toString(Double.valueOf(type.getAvg()), "0.00"), "sum", toString(Double.valueOf(type.getSum()), "0.00"), "tps", toString(Double.valueOf(type.getTps()), "0.00") });

/* 332 */     if (!type.getNames().isEmpty()) {
/* 333 */       for (Name name : type.getNames().values()) {
/* 334 */         name.accept(this.m_visitor);
      }
    }

/* 338 */     endTag("type");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/cross/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */